package com.senseuni.da.utils;

import android.content.res.Configuration;

import com.elvishew.xlog.XLog;
import com.senseuni.da.model.Language;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Locale;

/**
 * ================================================
 * Copyright (c) 2020 All rights reserved
 * 描述：
 * Author: Yuri
 * Date： Date: 2024/12/13
 * ================================================
 */
public class LanguageHelper {

    public static void changeSystemLanguage(Locale newLocale) {
        try {
            Class<?> activityManagerNativeClass = Class.forName("android.app.ActivityManagerNative");
            Method getDefaultMethod = activityManagerNativeClass.getMethod("getDefault", new Class<?>[0]);
            Object activityManagerNative = getDefaultMethod.invoke(null, new Object[0]);

            Method getConfigurationMethod = activityManagerNative.getClass().getMethod("getConfiguration", new Class<?>[0]);
            Configuration configuration = (Configuration) getConfigurationMethod.invoke(activityManagerNative, new Object[0]);

            configuration.locale = newLocale;

            Field userSetLocaleField = Configuration.class.getDeclaredField("userSetLocale");
            userSetLocaleField.setAccessible(true);
            userSetLocaleField.setBoolean(configuration, true);

            Method updateConfigurationMethod = activityManagerNative.getClass().getMethod("updateConfiguration", Configuration.class);
            updateConfigurationMethod.invoke(activityManagerNative, configuration);
            XLog.d("System language changed to: " + newLocale.getDisplayName());

        } catch (Exception e) {
            XLog.e( "Failed to change system language:%s", e.getMessage());
        }
    }

    public static String getLanguage() {
        String value =  Locale.getDefault().toString();
        if (value.equals("en")) {
            return "en_US";
        }
        return value;
    }

    public static String getLanguageName(String key) {
        switch (key) {
            case "in_ID":
                return "Indonesia";
            case "zh_CN":
                return "简体中文";
            default:
                return "English";
        }
    }

    public static void switchLanguage(String value) {
        XLog.d("switchLanguage:%s", value);
//        Context context = MyApplication.app.getApplicationContext();
        if (value.equals(getLanguage())) {
            XLog.d("same language,ignore...");
            return;
        }
        if (Language.ZH_CN.getValue().equals(value)) {
            switchCN();
        } else if (Language.EN_US.getValue().equals(value)) {
            switchEN();
        } else if (Language.IN_ID.getValue().equals(value)) {
            switchID();
        }
    }

    //印度尼西亚
    public static void switchID() {
        Locale local = new Locale("in", "ID");
        changeSystemLanguage(local);
    }

    public static void switchCN() {
        changeSystemLanguage(Locale.SIMPLIFIED_CHINESE);
    }

    public static void switchEN() {
        changeSystemLanguage(Locale.ENGLISH);
    }

}
